import { find, Label, Sprite, SpriteFrame, view } from 'cc';
import { tween } from 'cc';
import { UITransform } from 'cc';
import { Tween } from 'cc';
import { _decorator, Component, Node } from 'cc';
import { App } from '../../../App';
import UtilTool from '../../../tools/UtilTool';
import { GamePlayState } from '../GameBaseLogic';
import { GameLayer4 } from './GameLayer4';
import { GameModel4 } from './GameModel4';
const { ccclass, property } = _decorator;

// 猜拳界面
@ccclass('GameFourMora')
export class GameFourMora extends Component {
    @property(Node)
    leftHandNode : Node = null

    @property(Node)
    rightHandNode : Node = null

    @property(SpriteFrame)
    handSpr = []

    private leftIndex = 0
    private isStartPlay = false

    start() {
        this.node.getComponent(UITransform).setContentSize(view.getVisibleSize())

        this.playAction()
        setTimeout(() => {
            if (this && this.node) {
                this.startPlay(App.Model.gameView as GameModel4, true)
            }
        }, 3000);

        // 玩家地址
        let address1 = find("Node1/address",this.node).getComponent(Label)
        let address2 = find("Node2/address",this.node).getComponent(Label)
        let head1 = find("Node1/head",this.node)
        let head2 = find("Node2/head",this.node)

        setTimeout(() => {
            let gameView = App.Model.gameView as GameModel4
            let leftUserAddress = gameView.getGameLayerCtrl(GameLayer4).leftUserAddress
            let rightUserAddress = gameView.getGameLayerCtrl(GameLayer4).rightUserAddress
            address1.string = UtilTool.formatNickOrAddress({"nickname":App.Model.gameManage.gameStartData.nicknameMap[leftUserAddress], "address":leftUserAddress})
            address2.string = UtilTool.formatNickOrAddress({"nickname":App.Model.gameManage.gameStartData.nicknameMap[rightUserAddress], "address":rightUserAddress})

            // 头像
            this.updateHead(head1, App.Model.gameManage.gameStartData.headIdMap[leftUserAddress])
            this.updateHead(head2, App.Model.gameManage.gameStartData.headIdMap[rightUserAddress])
        }, 300)
        
    }

    // 更新头像
    updateHead(headNode, headId) {
        let headDict = App.Scene.getHallRes("images/head")
        if (headDict["head_" + headId]) {
            headNode.getComponent(Sprite).spriteFrame = headDict["head_" + headId]
        }
    }

    /**
     * 猜拳动画
     */
    playAction() {
        let rightIndex = 1
        let embedTween = tween(this.leftHandNode)
            .delay(0.1)
            .call(() => {
                this.leftIndex++
                rightIndex++

                if (this.leftIndex>2) {
                    this.leftIndex = 0
                }
                if (rightIndex>2) {
                    rightIndex = 0
                }
                this.leftHandNode.getComponent(Sprite).spriteFrame = this.handSpr[this.leftIndex]
                this.rightHandNode.getComponent(Sprite).spriteFrame = this.handSpr[rightIndex]
            })
        tween(this.leftHandNode)
            .repeatForever(embedTween)
            .start()
    }

    /**
     * 开始打球，关闭本界面
     */
    startPlay(gameView:GameModel4, isAuto=false) {
        gameView.setAllBallShow(true)
        if (this.isStartPlay) {
            gameView.moraPanel = null
            if (this && this.node) {
                this.node.destroy()
            }
            return
        }

        this.isStartPlay = true
        let currentHitPlayer = App.Model.gameManage.gameStartData.currentHitPlayer  // 猜拳赢的玩家
        let firstHitBallPlayerFinger = App.Model.gameManage.gameStartData.firstHitBallPlayerFinger // 猜拳胜利玩家出的结果 0，1，2 剪刀石头布
        let leftUserAddress = gameView.getGameLayerCtrl(GameLayer4).leftUserAddress
        Tween.stopAllByTarget(this.leftHandNode)

        let leftIndex = 0
        let rightIndex = 0
        if (currentHitPlayer==leftUserAddress){
            // 左边玩家先打
            leftIndex = firstHitBallPlayerFinger
            rightIndex = this.getLoseUser(firstHitBallPlayerFinger)
        }
        else{
            // 右边玩家先打
            rightIndex = firstHitBallPlayerFinger
            leftIndex = this.getLoseUser(firstHitBallPlayerFinger)
        }

        this.leftHandNode.getComponent(Sprite).spriteFrame = this.handSpr[leftIndex]
        this.rightHandNode.getComponent(Sprite).spriteFrame = this.handSpr[rightIndex]

        // if (!isAuto) {
            setTimeout(() => {
                gameView.moraPanel = null
                if (this && this.node) {
                    this.node.destroy()
                }
            }, 1000);
        // }

        gameView.gameLogic.setPlayState(GamePlayState.GAME_START)
    }

    /**
     * 获取输玩家的下标 index[0]剪刀 [1]石头 [2]布
     * @param winIndex 赢玩家的下标
     */
    getLoseUser(winIndex){
        let loseIndex = 0
        if (winIndex==0) { // 剪刀
            loseIndex = 2
        }
        else if (winIndex==1) { //石头
            loseIndex = 0
        }
        else if (winIndex==2) { //布
            loseIndex = 1
        }

        return loseIndex
    }

    onDestroy() {
        
    }

}

